package com.station.utils;

import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.RepoError;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.entity.SuccessEntity;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 金蝶云星空接口api
 * @Author: dongfangyu
 * @Date: 2025/9/17
 */
@Slf4j
public class KingDeeUtils {


    /**
     * 查询接口
     * @param formId 业务对象标识
     * @param jsonData json数据
     * @return
     */
    public static RepoResult view(String formId, String jsonData){
        K3CloudApi client = new K3CloudApi();
        try {
            String resultJson = client.view(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                log.info("接口返回结果:{} ", gson.toJson(repoRet.getResult()));
                return repoRet.getResult();
            } else {
                log.info("接口返回结果:{} " + gson.toJson(repoRet.getResult().getResponseStatus()));
                return repoRet.getResult();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存接口
     * @param formId 业务对象标识
     * @param jsonData json数据
     */
    public static RepoResult save(String formId, String jsonData){
        try {
            K3CloudApi client = new K3CloudApi();
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                log.info("接口返回结果: {} ", gson.toJson(repoRet.getResult()));
                return repoRet.getResult();
            } else {
                log.info("接口调用失败: {} " + gson.toJson(repoRet.getResult().getResponseStatus()));
                //接口返回结果: {"ErrorCode":"500","IsSuccess":false,"Errors":[{"Message":"\n系统中已存在相同编码03.01.01.986的物料，保存失败，请修改。","DIndex":0}],"SuccessEntitys":[]}
                return repoRet.getResult();
            }
        } catch (Exception e) {
            log.error("接口调用失败：{} " + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量保存接口
     * @param formId
     * @param jsonData
     * @return
     */
    public static RepoResult batchSave(String formId, String jsonData){
        K3CloudApi client = new K3CloudApi();
        try{
            //调用接口
            String resultJson = client.batchSave(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                log.info("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));
                return repoRet.getResult();
            } else {
                log.info("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
                return repoRet.getResult();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 执行单据查询接口
     * @param jsonData
     * @return
     */
    public static List<List<Object>> executeBillQuery(String jsonData) {
        K3CloudApi client = new K3CloudApi();
        try {
            List<List<Object>> resultList = new ArrayList<>();
            resultList = client.executeBillQuery(jsonData);
            return resultList;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    //处理日志成字符串
    public static String toErrorMessageString(ArrayList<RepoError> list){
        StringBuilder stringBuilder = new StringBuilder();
        for (RepoError repoError : list) {
            stringBuilder.append(repoError.getMessage()).append(",");
        }
        return  stringBuilder.toString();
    }

    /**
     * 批量审批
     * @param formId
     * @param jsonData
     * @return
     */
    public static RepoResult batchApprove(String formId, String jsonData){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        //String jsonData = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
        try{
            //业务对象标识
            //String formId = "SAL_RETURNSTOCK";
            //调用接口
            String resultJson = client.audit(formId,jsonData);

            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));
                return repoRet.getResult();
            } else {
                log.info("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
                return repoRet.getResult();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量提交
     * @param formId
     * @param jsonData
     * @return
     */
    public static RepoResult batchSubmit(String formId, String jsonData){
        K3CloudApi client = new K3CloudApi();
         try{
            //业务对象标识
            //String formId = "SAL_RETURNSTOCK";
            //调用接口
            String resultJson = client.submit(formId,jsonData);

            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));
                return repoRet.getResult();
            } else {
                log.info("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
                return repoRet.getResult();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
